/*
============================================================================
 Name        : BlockScrollInfo.cpp
 Author      : 
 Version     :
 Copyright   : Your copyright notice
 Description : CBlockScrollInfo implementation
============================================================================
*/

#include <eStock_L2hy.rsg>
#include <utf.h>

#include "eStockappui.h"
#include "json/inc/sjson.h"
#include "eStockMainGridContainer.h"
#include "BaseCommon.h"
#include "BlockInfoText.h"

#include "BlockScrollInfo.h"

using namespace sjson;


_LIT8(KScrollInfoURL, "Admin/EMCmsInfo.aspx?usertype=1&infotype=1&dt=1900-01-01%2000:00");
const TInt KPeriodicTimerInterval500 = 100000;

CBlockScrollInfo::CBlockScrollInfo():
CBlock(NULL)
{
	m_bHyConnect = ETrue;
}

CBlockScrollInfo::~CBlockScrollInfo()
{
	m_vRollInfo.ResetAndDestroy();
	m_vRollInfo.Close();
	if(m_AnimationTimer)
	{
		delete m_AnimationTimer;
		m_AnimationTimer = NULL;
	}

	if(m_pstrSummary != NULL)
	{
		m_pstrSummary->Reset();
		delete m_pstrSummary;
	}
	if(m_pstrTime != NULL)
	{
		m_pstrTime->Reset();
		delete m_pstrTime;
	}
	
	CEikonEnv::Static()->ScreenDevice()->ReleaseFont(iFont);
}

CBlockScrollInfo* CBlockScrollInfo::NewLC()
{
	CBlockScrollInfo* self = new (ELeave)CBlockScrollInfo();
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
}

CBlockScrollInfo* CBlockScrollInfo::NewL()
{
	CBlockScrollInfo* self=CBlockScrollInfo::NewLC();
	CleanupStack::Pop(); // self;
	return self;
}

void CBlockScrollInfo::ConstructL()
{
	CBlock::ConstructL();
	m_nInfoRollTime = 0;

	m_rect.SetRect(TPoint(0, m_nScreenHeight - KCbabarHeight - KScrollInfobarHeight), TSize(m_nScreenWidth, KScrollInfobarHeight));

	CGlobal::GetDesFromID(m_strDefaultRoll,LOC_BUF_STR_DEFAULTROLL);
	m_strDraw.Copy(m_strDefaultRoll);

	CGlobal::GetDesFromID(m_strStarKey,LOC_BUF_STR_VIEWROLLINFO);

	m_nYOffset = 0;
	m_nXOffset = 0;
	m_nCurIndex = 0;

	m_AnimationTimer = CPeriodic::NewL(CActive::EPriorityIdle);

	m_pstrSummary =  new (ELeave) CDesCArrayFlat(16);
	m_pstrTime =  new (ELeave) CDesCArrayFlat(16);
	
	CGraphicsDevice* screenDevice = CEikonEnv::Static()->ScreenDevice();
	TFontSpec myFontSpec = CEikonEnv::Static()->NormalFont()->FontSpecInTwips();
	myFontSpec.iHeight = screenDevice->VerticalPixelsToTwips(m_rect.Height() - 6);
	screenDevice->GetNearestFontInTwips(iFont, myFontSpec);
}

void CBlockScrollInfo::ShowScrollInfo(TBool aShow)
{
	m_pGlobal->iShowScrollInfo = aShow;
	GetContainer()->SizeChanged();
}

void CBlockScrollInfo::OnDraw(CFbsBitGc& g)
{
	if (!m_pGlobal->iShowScrollInfo)
		return;

//	g.SetBrushColor(TRgb(41, 46, 49));
//	g.SetBrushStyle(CGraphicsContext::ESolidBrush);
//	g.SetPenStyle(CGraphicsContext::ENullPen);
//	g.DrawRect(m_rect);
	
	g.SetClippingRect(m_rect);
	TInt y = m_rect.iBr.iY-m_nYOffset;
	TInt nWidthChar = iFont->TextWidthInPixels(m_strDraw);
	if(m_nYOffset>m_nLineHeight || nWidthChar <= m_rect.Width())
		y =  m_rect.iTl.iY;

	g.UseFont(iFont);
	g.SetPenStyle(CGraphicsContext::ESolidPen);
	g.SetBrushStyle(CGraphicsContext::ENullBrush);
	g.SetPenColor(KRgbScrollInfoText);
	g.DrawText(m_strDraw, TPoint(m_rect.iTl.iX - m_nXOffset, y + iFont->HeightInPixels() + 1));
	g.DiscardFont();
	
	g.CancelClippingRect();
}

void CBlockScrollInfo::OnKeyEvent(TInt keyCode)
{
	if (!m_pGlobal->iShowScrollInfo)
		return;

	if (keyCode =='*' || keyCode == EStdKeyNkpAsterisk )
	{
		if(m_nCurIndex >=0 && m_nCurIndex < m_vRollInfo.Count() )
		{
			CBlockInfoText* it = CBlockInfoText::NewL((CBlock*)GetContainer()->m_block);
			
			it->m_strTextTitle = m_vRollInfo[m_nCurIndex]->m_pTitle->Alloc();
			it->m_strText = m_vRollInfo[m_nCurIndex]->m_pText->Alloc();
			it->InitData();
			GetContainer()->AddBlock(it);
		}
	}
}

void CBlockScrollInfo::SizeChanged()
{
	if (!m_pGlobal->iShowScrollInfo)
	{
		m_rect.SetRect(0, 0, 0, 0);
		return;
	}
	if (m_pGlobal->g_nMobileOS == 5 && m_pGlobal->iWideScreen)
	    m_rect.SetRect(TPoint(0, m_nScreenHeight - KTabbarHeight - KScrollInfobarHeight), TSize(m_nScreenWidth, KScrollInfobarHeight));
	else
	    m_rect.SetRect(TPoint(0, m_nScreenHeight - KCbabarHeight - KScrollInfobarHeight), TSize(m_nScreenWidth, KScrollInfobarHeight));

	if (m_nYOffset>m_nLineHeight)
		m_nYOffset = m_nLineHeight;
}
/*
TBool CBlockScrollInfo::ReadData(const TDesC8& aData)
{
	TPtrC8 ptr(aData);

	TInt16 sInfoType = CGlobal::ReadInt16L(ptr);

	TInt8 bStatus = CGlobal::ReadInt8L(ptr);
	if ((bStatus&1)!=0)
	{
		m_vRollInfo.Reset();

		m_nInfoRollTime = CGlobal::ReadInt32L(ptr);

		TInt16 sRecordNum = CGlobal::ReadInt16L(ptr);

		for(TInt k=0;k<sRecordNum;k++)
		{
			TInt64 lOffset = CGlobal::ReadInt64L(ptr);
			TInt nLength = CGlobal::ReadInt32L(ptr);
			HBufC* strTitle = CGlobal::ReadStringL(ptr);
			HBufC* strTime = CGlobal::ReadStringL(ptr);
			HBufC* strSummary = CGlobal::ReadStringL(ptr);
			m_pstrSummary->AppendL(strSummary->Des());
			m_pstrTime->AppendL(strTime->Des());
			strTitle->Des().TrimAll();
			TInfo info(sInfoType, 0, lOffset, nLength, strTitle->Des());
			delete strTitle;
			delete strTime;
			delete strSummary;
			m_vRollInfo.Append(info);
		}
		m_bExit = ETrue;
		m_nXOffset = 1000;
	}
	return ETrue;
}

void CBlockScrollInfo::WriteData(TDes8& aBuf)
{
	CGlobal::WriteInt32L(aBuf,m_nInfoRollTime);
	CGlobal::WriteInt8L(aBuf,(TInt8)m_nLinesPerPage);
	CGlobal::WriteInt32L(aBuf,m_nOnlineDays);
}

TInt CBlockScrollInfo::GetDataLength()
{
	return 9;
}

TInt16 CBlockScrollInfo::GetDataType()
{
	return RequestDataType_RollInfo;
}
*/
TInt CBlockScrollInfo::OnTitleRoll(TAny* aAny)
{
	CBlockScrollInfo* self = static_cast<CBlockScrollInfo*>( aAny );
	self->UpdatePosition();
	return KErrNone;
}

void CBlockScrollInfo::UpdatePosition()
{
	TInt nWidthChar = iFont->TextWidthInPixels(m_strDraw);
	if (m_nYOffset < m_rect.Height())
	{
		m_nYOffset += 1;
		if (nWidthChar < m_rect.Width())
		    return;
	}
	else
		m_nXOffset += 2;
	
	if(m_nXOffset > nWidthChar-m_rect.Width())
	{
		m_nYOffset = 0;
		m_nXOffset = 0;
		//m_nXOffsetDraw = 0;
		m_nCurIndex ++;
		if(m_nCurIndex >= m_vRollInfo.Count() )
			m_nCurIndex = 0;

		if(m_vRollInfo.Count() > 0 )
		{
			if(NULL != m_vRollInfo[m_nCurIndex]->m_pTitle)
				m_strDraw.Copy(*m_vRollInfo[m_nCurIndex]->m_pTitle);
			if (!m_pGlobal->m_bSupportTouch)
				m_strDraw.Append(m_strStarKey);
		}
		else
			m_strDraw.Copy(m_strDefaultRoll);
	}

	DrawNow(m_rect);
}


void CBlockScrollInfo::StartScroll()
{
	m_AnimationTimer->Cancel();
	m_AnimationTimer->Start(
		KPeriodicTimerInterval500, KPeriodicTimerInterval500, 
		TCallBack(OnTitleRoll, this));
}

TBool CBlockScrollInfo::GetHYURL(TDes8& aUri)
{
	CBlock::GetHYURL(aUri);
	aUri.Append(KScrollInfoURL);
	return ETrue;
}

TBool CBlockScrollInfo::ParseJSONArray(const TDesC8& aJson)
{
	CJSONValue* pValue = CJSONValue::NewL();
    TInt ret = pValue->Parse(aJson);
    CJSONArray& v = pValue->Get<CJSONArray>();
    TInt nIndex = 0;
    TBuf<1024> buf;
    TBool b = EFalse;
    for(nIndex = 0; v.Has<CJSONObject>(nIndex) ;nIndex ++, b = EFalse)
    {
    	CHYInfoData* pInfoData = new(ELeave)CHYInfoData();
    	CJSONObject& o = v.Get<CJSONObject>	(nIndex);
    	if(o.Has<TDesC8>(_L8("title")))
    	{
    		TDesC8& name = o.Get<TDesC8>(_L8("title"));
    		buf.Zero();
    		CnvUtfConverter::ConvertToUnicodeFromUtf8(buf, name);
    		pInfoData->m_pTitle = buf.Alloc();
    	}
    	if(o.Has<TDesC8>(_L8("content")))
    	{
    		TDesC8& name = o.Get<TDesC8>(_L8("content"));
    		buf.Zero();
    		CnvUtfConverter::ConvertToUnicodeFromUtf8(buf, name);
    		pInfoData->m_pText = buf.Alloc();
    		b = ETrue;
    	}
    	if(b)
    		m_vRollInfo.Append(pInfoData);
    	else
    		delete pInfoData;
    }
    
    if(nIndex > 0)
    {
    	m_bExit = ETrue;
		m_nXOffset = 1000;	
    }

    delete pValue;
    return ETrue;
}

CHYInfoData::CHYInfoData()
{
	m_pTitle = NULL;
	m_pText = NULL;
}

CHYInfoData::~CHYInfoData()
{
	delete m_pTitle;
	delete m_pText;
}
